package com.ruoyi.im.task;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.ruoyi.im.config.ServerClusterConfig;
import com.ruoyi.im.controller.ServerEndPoint;
import com.ruoyi.im.entity.MsgRoute;
import com.ruoyi.im.service.OffLineMsgService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;

@Component
public class MsgLoopPusherTask {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private OffLineMsgService offLineMsgService;


    @RabbitListener(queues = {"#{@queueName}"},ackMode ="MANUAL")
    public void run1(Message message, Channel channel, String msg) {
        System.out.println("推送消息1" + msg.toString());
        try {
            //对消息进行推送
            MsgRoute msgRoute = JSON.parseObject(msg, MsgRoute.class);
            routeMsg(msgRoute, msg);

            //手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    @RabbitListener(queues = {"#{@queueName}"},ackMode ="MANUAL")
    public void run2(Message message, Channel channel, String msg) {
        System.out.println("推送消息2" + msg.toString());
        try {
            //对消息进行推送
            MsgRoute msgRoute = JSON.parseObject(msg, MsgRoute.class);
            routeMsg(msgRoute, msg);

            //手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    /**
     * 发送消息
     *
     * @param msgRoute
     * @param msg
     */
    private void routeMsg(MsgRoute msgRoute, String msg) {
        //所有在线用户
        Map<Integer, Session> onlineSessionList = ServerEndPoint.onlineSessionList;
        Integer receiverId = msgRoute.getReceiverId();
        Session session = onlineSessionList.get(receiverId);
        if (session == null || !session.isOpen()) {
            //当前用户不存在，存放到离线消息中
            offLineMsgService.addNewOfflineMsg(msgRoute);
            return;
        }

        try {
            session.getBasicRemote().sendText(msg);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
